<?php
// cps管理
namespace Admin\Controller;
use Think\Controller;
class AdCpsController extends Controller
{
	public function __construct($value='')
	{
		$this->isAjaxLogin();
	}
	 
	// cps添加或修改
	public function CpsChange($value='')
	{

		$id=I('post.id/d');
		$ad_id=I('post.ad_id/d');

		$start_flow=I('post.start_flow/f');

		$CpsRule=D('Cps');
		$data=I('post.');
		
		$data=$CpsRule->field('id',true)->create($data);//广告主
	 	 if (!$data) {
	 	 	$result['msg']=$CpsRule->getError();
	 	 	$result['code']=500;
			$this->ajaxReturn($result);
	 	 }





	 	if (empty($id)) {

		 	 if (empty($ad_id)) {
		 	 	 $res=$CpsRule->checkAddCps($start_flow);
		 	 }else{
		 	 	 $res=$CpsRule->checkSaveCps($ad_id,$start_flow);
		 	 }

		 	 if (!$res) {
		 	 	$result['msg']='cps起始规则必须大于上一条规则结束流水';
		 	 	$result['code']=500;
		 	 	$this->ajaxReturn($result);
		 	 }

		 	 
	 		if (empty($ad_id)) {

				$data['session_id']=session_id();			 			 
	 		}else{
				$data['ad_id']=$ad_id;
	 		}


			$data['user_type']='1';
			$data['is_delete']=0;
			$data['add_time']=time();

			$res=($data&&$CpsRule->add($data)); 	
			if ($res) { 
				
				if (!empty($ad_id)) session('ad_'.$ad_id,1);
				
				$result['msg']='添加成功';		 
				$result['code']=200;		 
			}else{
				$result['msg']='添加失败';	
				$result['code']=501;	
			} 
	 	}else{

	 		
	 		unset($data['ad_id']);
			$res=($data&&$CpsRule->field('ad_id',true)->where(['id'=>$id])->save($data));

			$ad_id=$CpsRule->where(['id'=>$id])->getField('ad_id');

			if ($res) { 
				if (!empty($ad_id)) {
					session('ad_'.$ad_id,1);	 
	 			}
	 			
				$result['msg']='修改成功';
				$result['code']=200;		 
			}else{
				$result['msg']='修改失败';	
				$result['code']=502;		 
			} 
	 	}

	 	$this->ajaxReturn($result);
		 
	}
	
	

	// 广告cps列表
	public function cpsList($value='')
	{
		$ad_id=I('get.ad_id','');
		
		$CpsRule=M('cps_rule');

		if (C('IS_FALSE_DEL')) {
        	$where['is_delete']='0';	 
        } 

        if (empty($ad_id)) {
    
			$where['session_id']=session_id();
			$res=$CpsRule->where($where)->select();	
			 
		}else{
			$where['ad_id']=$ad_id;
			$res=$CpsRule->where($where)->select();
		}
		
		// if (!empty($res)) {
			$result['msg']='查询成功';
			$result['code']=200;
			$result['data']=$res;
		// }else{
		// 	$result['msg']='查询失败';
		// 	$result['code']=500;
		// 	$result['data']=[];
		// }

		$this->ajaxReturn($result);
	}

	// 广告cps删除
	public function cpsDel($value='')
	{
		 $id=I('get.id/d');
		  if (!C('IS_FALSE_DEL')) {
		 	$res=M('cps_rule')->where(['id'=>$id])->delete();
		 }else{
		 	$res=M('cps_rule')->where(['id'=>$id])->save(['is_delete'=>'1']);
		 }

		$ad_id=M('cps_rule')->where(['id'=>$id])->getField('ad_id');
		if($res) session('ad_'.$ad_id,1);
		
		 $result['msg']=$res ? '删除成功' : '删除失败';
		 $result['code']=$res ? 200 : 500;
		 $this->ajaxReturn($result);
		 	 
	}
	// cps修改渲染
	public function cpsInfo($value='')
	{
		$id=I('get.id/d');
		$res=M('cps_rule')->where(['id'=>$id])->find();
		// if (!empty($res)) {
			$result['msg']='查询成功';
			$result['code']=200;
			$result['data']=$res;
		// }else{
		// 	$result['code']=500;
		// 	$result['msg']='查询失败';
		// }
		
		$this->ajaxReturn($result);
	}
	// 清楚cps临时数据
	public function cpsClear($value='')
	{
		$res=M('cps_rule')->where(['session_id'=>session_id()])->delete();
		// if ($res) {
	    	$result['msg']='删除成功';
	    	$result['code']=200;
		// }else{
	 //    	$result['msg']='删除成功';
	 //    	$result['code']=500;
		// }
		
		$this->ajaxReturn($result);
	}

}